#!/bin/bash
PROG_NAME=$0
ACTION=$1
ENV=$2



#APP_HOME=/home/admin/ykt/${APP_NAME} # 从package.tgz中解压出来的jar包放到这个目录下
JAR_NAME=${APP_NAME}.jar # jar包的名字
APP_LOG=/dev/null # 应用的日志文件
APP_PATH=${APP_HOME}/${JAR_NAME}
# 创建出相关目录
mkdir -p ${APP_HOME}
mkdir -p ${APP_HOME}/logs
usage() {
    echo "Usage: $PROG_NAME {start|stop|restart}"
    exit 2
}

start_application() {
	rm -f ${APP_PATH}
	cp ${APP_HOME}/application/${JAR_NAME} ${APP_PATH}
    echo "starting ${APP_NAME}"
    nohup java -jar -Xms2048m -Xmx2048m -Duser.timezone=Asia/Shanghai ${APP_PATH} --spring.profiles.active=${profiles} > ${APP_LOG} &
}
stop_application() {
    echo "stopping ${APP_NAME}"

    PID=`ps ax | grep ${APP_PATH} | grep -v grep | awk '{print $1}'`
    echo ${PID}
    if [[ ! -z "$PID" ]]; then
    kill -9 $PID
    else
     echo "${APP_NAME} is not running"
    fi
}
start() {
    start_application
}
stop() {
    stop_application
}
case "$ACTION" in
    start)
        stop
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    *)
        usage
    ;;
esac